package com.baoxin.chenxixc4coach.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.GlideApp;
import com.baoxin.chenxixc4coach.R;
import com.baoxin.chenxixc4coach.bean.News;
import com.baoxin.chenxixc4coach.bean.UncheckStudent;
import com.baoxin.chenxixc4coach.java.FooterViewHolder;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class UnckechAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<UncheckStudent> uncheckStudents;
    private Context context;
    private CheckListener checkListener;

    private int normalType = 0;
    private int footType = 1;
    private boolean hasMore;//是否有更多数据

    public UnckechAdapter(Context context, CheckListener checkListener) {
        this.context = context;
        this.checkListener = checkListener;
    }

    public void updateData(List<UncheckStudent> uncheckStudents, boolean hasMore) {
        this.uncheckStudents = uncheckStudents;
        this.hasMore = hasMore;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == normalType) {
            View view = LayoutInflater.from(context).inflate(R.layout.uncheck_item, parent, false);
            final MyViewHolder holder = new MyViewHolder(view);
            return holder;
        } else {
            View footerView = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
            ProgressBar loadingPb = footerView.findViewById(R.id.loading_pb);
            TextView noMoreData = footerView.findViewById(R.id.no_more_data);
            if (hasMore) {
                loadingPb.setVisibility(View.VISIBLE);
                noMoreData.setVisibility(View.GONE);
            } else {
                loadingPb.setVisibility(View.GONE);
                noMoreData.setVisibility(View.VISIBLE);
            }
            FooterViewHolder footerViewHolder = new FooterViewHolder(footerView);
            return footerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MyViewHolder) {
            final UncheckStudent student = uncheckStudents.get(position);
            GlideApp.with(context)
                    .load(student.getPhotoUrl())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .error(R.drawable.default_)
                    .into(((MyViewHolder) holder).photo);
            ((MyViewHolder) holder).name.setText(student.getUserName());
            ((MyViewHolder) holder).phone.setText(student.getPhone());
            int state = student.getCheckState();
            if (state == 0) {
                ((MyViewHolder) holder).status.setVisibility(View.GONE);
                ((MyViewHolder) holder).prll.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).pass.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //     checkListener.onPass(student);
                        checkListener.onPass(position);
                    }
                });
                ((MyViewHolder) holder).refuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //      checkListener.onRefuse(student);
                        checkListener.onRefuse(position);
                    }
                });
            } else {
                ((MyViewHolder) holder).status.setVisibility(View.VISIBLE);
                ((MyViewHolder) holder).prll.setVisibility(View.GONE);
                if (state == 1) {
                    ((MyViewHolder) holder).status.setText("已通过");
                    Drawable pass = context.getResources().getDrawable(R.drawable.uncheck_pass);
                    pass.setBounds(0, 0, pass.getMinimumWidth(), pass.getMinimumHeight());
                    ((MyViewHolder) holder).status.setCompoundDrawables(pass, null, null, null);
                } else {
                    ((MyViewHolder) holder).status.setText("已拒绝");
                    Drawable pass = context.getResources().getDrawable(R.drawable.uncheck_refuse);
                    pass.setBounds(0, 0, pass.getMinimumWidth(), pass.getMinimumHeight());
                    ((MyViewHolder) holder).status.setCompoundDrawables(pass, null, null, null);
                }
            }

        } else {
            if (hasMore) {
                ((FooterViewHolder) holder).mPb.setVisibility(View.VISIBLE);
                ((FooterViewHolder) holder).mNoMoreData.setVisibility(View.GONE);
            } else {
                ((FooterViewHolder) holder).mPb.setVisibility(View.GONE);
                ((FooterViewHolder) holder).mNoMoreData.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1) {
            return footType;
        } else {
            return normalType;
        }
    }

    @Override
    public int getItemCount() {
        return uncheckStudents == null ? 0 : uncheckStudents.size() + 1;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        View itemView;
        ImageView photo;
        TextView name;
        TextView phone;
        TextView time;
        TextView status;//同意or拒绝
        LinearLayout prll;
        Button pass;
        Button refuse;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.itemView = itemView;
            photo = itemView.findViewById(R.id.photo);
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
            time = itemView.findViewById(R.id.time);
            status = itemView.findViewById(R.id.status);
            prll = itemView.findViewById(R.id.prll);
            pass = itemView.findViewById(R.id.pass);
            refuse = itemView.findViewById(R.id.refuse);

        }
    }

    public interface CheckListener {
        void onPass(int position);

        void onRefuse(int position);
    }

}
